/**
 * Created by zhaojm on 15/3/17.
 */
game.MosterLayer = cc.Layer.extend({

    _mosterList : null,

    ctor:function(){
        this._super();

        this._mosterList = [];

        var size = cc.winSize;

        that = this;


        for(;;){
            var moster = this.addOneMoster();
            if(moster.getPositionX() > size.width){
                break;
            }
        }

        this.scheduleUpdate();

    },

    update:function(dt){
        this._super(dt);

        // TODO 检查 障碍物的 删除和创建

        while(true){

            if(this._mosterList[0].getPositionX() + this._mosterList[0].getContentSize().width <= -this.parent.getPositionX()){
                //cc.log('removeMoster');
                this.removeChild(this._mosterList[0]);

                this._mosterList.splice(0, 1);

            }else{
                break;
            }
        }

        if(this._mosterList[this._mosterList.length - 1].getPositionX() + this.parent.getPositionX() < cc.winSize.width + 10){
            this.addOneMoster();
        }



        // TODO 碰撞检测，碰撞 死亡
        var rectHero = cc.rect(
            this.parent.hero.getPositionX() + 10,
            this.parent.hero.getPositionY() + 10,
            this.parent.hero.getContentSize().width - 10,
            this.parent.hero.getContentSize().height - 10
        );

        for (var i = 0; i < this._mosterList.length; i++) {

            var rectMoster = cc.rect(
                this._mosterList[i].getPositionX() + 10,
                this._mosterList[i].getPositionY() + 10,
                this._mosterList[i].getContentSize().width - 10,
                this._mosterList[i].getContentSize().height - 10
            );


            if (this.isCollided(rectHero, rectMoster) && this._mosterList[i].hadCollided == false) {
                // TODO to die

                //cc.log('heart num...', this.parent.heart);

                this.parent.heart--;
                this._mosterList[i].hadCollided = true;

                //cc.log('heart num...', this.parent.heart);

                this.parent.uiLayer.setHeart(this.parent.heart);

                if(this.parent.heart == 0){
                    //cc.log('heart num...', this.parent.heart);
                    this.parent.touchLayer.setTouchEnabled(false);

                    this.unscheduleUpdate();
                    this.parent.coinLayer.unscheduleUpdate();

                    this.parent.hero.unscheduleUpdate();

                    this.parent.unscheduleUpdate();

                    this.parent.hero.setAnchorPoint(cc.p(0.5, 0.5));
                    // 死亡动画
                    var time = 2;
                    var rotateBy = cc.RotateBy.create(time, 360 * 2);
                    var moveBy = cc.MoveBy.create(time, cc.p(500, 0));
                    var spawn = cc.Spawn.create(rotateBy, moveBy);
                    that = this;
                    var callfunc = cc.CallFunc.create(this.callBack);
                    var seq = cc.Sequence.create(spawn, callfunc);
                    this.parent.hero.runAction(seq);
                }else{
                    //var time = 0.5;
                    //var rotateBy = cc.RotateBy.create(time, 360);
                    //var moveBy = cc.MoveBy.create(time, cc.p(500, 0));
                    //var spawn = cc.Spawn.create(rotateBy, moveBy);
                    //that = this;
                    //var callfunc = cc.CallFunc.create(this.callBack);
                    //var seq = cc.Sequence.create(spawn, callfunc);
                    //this.parent.hero.runAction(rotateBy);
                    //var time = 0.05;
                    //var skew0 = cc.SkewTo.create(time, 40, 20);
                    //var skew1 = cc.SkewTo.create(time, -20, -40);
                    //var skew2 = cc.SkewTo.create(time, 40, 20);
                    //var skew3 = cc.SkewTo.create(time, -20, -40);
                    //var skew4 = cc.SkewTo.create(time, 0, 0);
                    //var seq = cc.Sequence.create(skew0, skew1, skew2, skew3, skew4);
                    //this.parent.hero.runAction(seq);


                    var time = 0.5;
                    var rotateBy = cc.RotateBy.create(time, 360);
                    //var moveTo = cc.MoveBy.create(time, cc.p(0, 20));

                    //var animation = cc.animationCache.getAnimation('collided');
                    //var animate = cc.Animate.create(animation);
                    this.parent.hero.isCollided = true;
                    that_in_moster_layer = this;
                    var callfunc = cc.CallFunc.create(function(){
                        //that_in_moster_layer.parent.hero._heroSprite.stopAllActions();
                        //that_in_moster_layer.parent.hero.runAnimation('idle');
                        that_in_moster_layer.parent.hero.isCollided = false;
                        that_in_moster_layer.parent.hero.speedY = 0;
                    });

                    //var spawn = cc.Spawn.create(rotateBy, moveTo);
                    var seq = cc.Sequence.create(rotateBy, callfunc);
                    //
                    //this.parent.hero._heroSprite.stopAllActions();
                    //this.parent.hero._heroSprite.runAction(seq);
                    this.parent.hero.runAction(seq);



                }

                break;
            }
        }


    },



    addOneMoster:function(){
        //cc.log('addOneMoster');

        var moster = this.createOneMoster();


        if(this._mosterList.length == 0){
            moster.setPosition(cc.p(cc.winSize.width * 3,   cc.winSize.height * game.ConfigHelper.getMosterYPercent()));
        }else{
            var lastMoster = this._mosterList[this._mosterList.length - 1];
            moster.setPosition(cc.p(
                lastMoster.getPositionX() + lastMoster.getContentSize().width + game.ConfigHelper.getMosterInterval(),
                cc.winSize.height * game.ConfigHelper.getMosterYPercent()
            ));
        }

        this.addChild(moster);
        this._mosterList.push(moster);

        return moster;
    },


    createOneMoster:function() {

        return new game.BaseMoster();
    },


    isCollided:function(aRect,bRect){
        return cc.rectIntersectsRect(aRect, bRect);
    },

    callBack:function(){
        var data = {};
            data.scoreNum = that.parent.scoreNum;
            data.distance = Math.floor(that.parent.distance);
            var gameOverScene = new game.GameOverScene();
            gameOverScene.setData(data);
            cc.director.runScene(gameOverScene);

    },



});